<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-11
 * Time: 下午12:41
 * To change this template use File | Settings | File Templates.
 */
class register extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();

        //if ($this->isLogin()) {
            //$this->load->helper('url');
            //redirect("/");
            //return ;
        //}
    }

    public function index()
    {
        $referer = $this->input->server('HTTP_REFERER');
        $referer = $referer ? $referer : '';
        $this->load->view('user/register', array('referer'=>$referer));
    }

    /**
     * 提交注册
     */
    public function submit()
    {
        $this->load->helper('validation');

        $username = $this->input->get_post('username');
        $password = $this->input->get_post('password');
        $repassword = $this->input->get_post('repassword');
        $verifyCode = $this->input->get_post('verify_code');
        $redirect_url = $this->input->get_post('redirect_url');
        $redirect_url = is_url($redirect_url) ? $redirect_url : '/';
        $source = $this->input->get_post('source');
        $source = $source ? $source : 1;
        $email = $this->input->get_post('email');
        $agree = $this->input->get_post('agree');

        $error = '';
        do {
            if (! $agree) {
                $error = '您没有同意网站服务条款';
                break;
            }

            if (!length_limit($username, 6, 32)) {
                $error = '用户名不合法';
                break;
            }
            if ($password !== $repassword) {
                $error = '两次输入密码不同';
                break;
            }

            if (!length_limit($password, 6, 32)) {
                $error = '密码长度6~32个字符';
                break;
            }

            if (!is_email($email)) {
                $error = 'E-MAIL 地址不合法';
                break;
            }

            if ('' === $verifyCode || md5(strtolower($verifyCode)) !== $this->getVerifyCode()) {
                $error = '验证码不正确';
                break;
            }

            $this->unsetVerifyCode();

        } while (false);

        if($error)
        {
            $this->message('注册失败', $error , 'javascript:history.back();');
        }
        else
        {
            self::include_uc_lib();
            $uid = uc_user_register($username, $password, $email, '', '', $this->input->ip_address());
            if($uid > 0) {
                $password = md5(uniqid('', true));
                $this->load->model('user/Model_User', 'user');
                $userId = $this->user->registerUser(array('user_id'=>$uid,'email'=>$email,'passport'=>$username,'password'=>($password)), true);
//echo $userId;exit;
                $this->load->helper('cookie');

                $expire = true ? time()+(86400 * 360) : 0; //是否记录登录状态
                set_cookie('auth', authcode("{$password}\t{$uid}", 'ENCODE'), $expire);

                $this->message('注册成功', '' , '/user/login');
            }
            else
            {
                switch($uid){
                    case -3:$error = '用户名重复';break;
                    case -6:$error = '邮箱重复';break;
                    default:$error = '未知错误';break;
                }
                $this->message('注册失败', $error , 'javascript:history.back();');
            }
        }
    }

    function verifycode()
    {
        $this->load->library('session');
        $this->load->helper('verifycode');
        $captcha = new Captcha(80,28,4);
        $captcha->showImg();
        $newdata = array('verifyCode'  => md5(strtolower($captcha->getCaptcha())));
        $this->session->set_userdata($newdata);
        return;
    }

    private function getVerifyCode()
    {
        $this->load->library('session');
        return $this->session->userdata('verifyCode');
    }

    private function unsetVerifyCode()
    {
        $this->load->library('session');
        $this->session->unset_userdata('verifyCode');
        return;
    }

    /**
     * 用户是否存在
     */
    public function userIsExist()
    {
        $passPort = $this->input->get_post('passport');
        $type = $this->input->get_post('type');

        $response = array('error' => '0', 'msg' => '用户名验证成功', 'code' => 'username_verify_success');

        do {
            if ( empty ($passPort) ) {
                $response = error(10010);
                break;
            }

            $this->load->model('user/Model_User', 'user');
            //$uInfo = $this->user->getUserInfoByPassport($passPort);
            $uInfo = $this->getUserInfo($passPort, $type);

            if ( !empty($uInfo) ) {
                $response = error(10002);
                break;
            }
        } while (false);

        self::json_output($response);
    }

    /**
     * 用户是否激活
     */
    public function isActivation()
    {
        $passPort = $this->input->get_post('passport');
        $type = $this->input->get_post('type');

        $response = array('error' => '0', 'msg' => '用户已激活', 'code' => 'username_activation');

        do {
            if ( empty($passPort) ) {
                $response = error(10010);
                break;
            }

            $uInfo = $this->getUserInfo($passPort, $type);

            if ( empty($uInfo) ) {
                $response = error(10006);
                break;
            }

            if ( empty ($uInfo['is_activation']) || $uInfo['is_activation'] == '0') {
                $response = error(10011);
                break;
            }
        } while (false);

        self::json_output($response, true);
    }

    /**
     * 获取用户信息
     *
     * @param $userName
     * @param $type
     * @return bool
     */
    private function getUserInfo($userName, $type)
    {
        if ( empty ($userName) ) {
            return false;
        }

        $this->load->model('user/Model_User', 'user');
        if ( $type ) {
            $uInfo = $this->user->getUserInfoByPassport($userName);
        } else {
            $uInfo = $this->user->getUserByNickName($userName);
        }
        return $uInfo;
    }
}
